"""
 -*- coding: utf-8-*-
 @File: lim_project_mgt.py
 @Author: chen
 @time: 2023/09/10/ 10:10
"""
import json
import requests


# Lim_项目管理
class LimProjectMgt:
    def __init__(self):
        self.host = "http://121.43.43.59:8006"
        self.header = {
            'Accept': 'application/json',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'Connection': 'keep-alive',
            'Authorization': 'Token 42418399e5a70df553f6189ee0a7fa98251109c2',
            'Content-Type': 'application/json;charset=UTF-8, application/json',
            'Origin': 'http: //121.43.43.59',
            'Referer': 'http: //121.43.43.59/'
        }

    def find_all_items(self, page: int = 1, page_size: int = 10):
        """
        :param page: 要查询的页码
        :param page_size: 分页大小
        :return: 返回当前页信息
        """
        path = '/project/project-view'
        url = "".join([self.host, path])
        params = {"page": page, "page_size": page_size}
        res = requests.request("get", url, params=params)
        return res

    def create_item(self, item_name: str):
        """
        创建新的项目
        :param item_name: 项目名称
        :return: 新项目的信息
        """
        path = '/project/project-view'
        url = "".join([self.host, path])
        request_body = {'name': item_name}
        json_body = json.dumps(request_body)
        res = requests.request("post", url=url, headers=self.header, data=json_body)
        return res

    def update_item(self, item_name: str, item_id: int, item_ip: str):
        """
        修改项目信息
        :param item_name: 要修改的项目名称
        :param item_id: 要修改的项目id
        :param item_ip: 修改的ip内容
        :return:返回修改后的项目信息
        """
        path = '/project/project-view'
        url = "".join([self.host, path])
        request_body = {'name': item_name, "envir_2_host": item_ip, "id": item_id}
        json_body = json.dumps(request_body)
        res = requests.request("patch", url=url, headers=self.header, data=json_body)
        return res

    def delete_item(self, item_id: int):
        """
        删除项目
        :param item_id: 根据项目id删除
        :return: 返回删除结果
        """
        path = "/project/project-view"
        url = self.host + path
        params = {"id": item_id}
        res = requests.request("delete", url=url, headers=self.header, params=params)
        # print(res.json())
        return res


if __name__ == '__main__':
    p1 = LimProjectMgt()
    print(p1.find_all_items().json())
    # p1.create_item()
    # p1.delete_item()
